package com.riguz.forks.framework.aop;

import java.lang.reflect.Method;

import com.riguz.forks.framework.BeanFactory;

import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;

public class ActionProxy implements MethodInterceptor{
    @Override
    public Object intercept(Object target, Method method, Object[] args, MethodProxy methodProxy) throws Throwable {
        Before before = method.getAnnotation(Before.class);
        After after = method.getAnnotation(After.class);
        if(before != null)
            runInterceptors(before.value());
        Object result = methodProxy.invokeSuper(target, args);
        if(after != null)
            runInterceptors(after.value());
        return result;
    }

    public void runInterceptors(Class<? extends Interceptor>[] interceptors){
        if(interceptors != null){
            for(Class<? extends Interceptor> i : interceptors){
                Interceptor interceptor = BeanFactory.getInstance(i);
                interceptor.intercept();
            }
        }
    }
}
